package com.ywj.collection.Collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StudentSort {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<>();
        list.add(new Student("Rose",18));
        list.add(new Student("Jack",16));
        list.add(new Student("LiHua",20));
        list.add(new Student("Jenny",18));

        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                //以学生的年龄升序，如果年龄一样 就按名字升序
                int result = o1.getAge() - o2.getAge();
                result = result==0 ? o1.getName().compareTo(o2.getName()) : result;
                return result;
            }
        });

        for (Student stu : list) {
            System.out.println(stu);
        }
    }
}
